import 'package:demo/app/widgets/dialog_key_value.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../../../model/constant_model.dart';
import '../../../../../model/contact_model.dart';
import '../../../../../model/key_value_model.dart';
import '../../../../../net/api_provider.dart';
import '../base_info_controller.dart';

class Tab3Controller extends GetxController {

  ApiProvider apiProvider = Get.find();
  final FocusNode focusNode = FocusNode();
  void hintKeyboard(){
    focusNode.unfocus(); // 触发焦点变化，键盘收回
  }

  @override
  void onReady() {
    super.onReady();
    getConstant(isFirst: true);
  }

  TextEditingController phone1Controller = TextEditingController(); /// 第一联系人手机号
  TextEditingController phone2Controller = TextEditingController(); /// 第二联系人手机号
  TextEditingController name1Controller = TextEditingController(); /// 第一联系人姓名
  TextEditingController name2Controller = TextEditingController(); /// 第二联系人姓名
  Map<String, dynamic>? relationList; /// 联系人关系
  Rx<KeyValue> selectRelation1 = KeyValue(0,'').obs; /// 选择的第一联系人关系
  Rx<KeyValue> selectRelation2 = KeyValue(0,'').obs; /// 选择的第二联系人关系

  /// 获取配置的列表
  void getConstant({bool isFirst = false}) async{
    var result = await apiProvider.getData({}, ApiProvider.constant);
    if(result.code == 200){
      var constant = Constant.fromJson(result.data);
      relationList = constant.relationList;
      if(isFirst){
        getContactDetail();
      }
    }
  }

  /// 获取联系人详情
  void getContactDetail() async {
    var result = await apiProvider.getData({}, ApiProvider.contact_detail);
    if(result.code == 200){
      var contact = Contact.fromJson(result.data);
      phone1Controller.text = contact.firstPhone;
      phone2Controller.text = contact.secondPhone;
      name1Controller.text = contact.firstName;
      name2Controller.text = contact.secondName;
      if(contact.firstRelation > 0 && relationList != null){
        for(var entry in relationList!.entries) {
          if (entry.key == '${contact.firstRelation}'){
            selectRelation1.value = KeyValue(int.tryParse(entry.key) ?? 0,entry.value);
          }
        }
      }
      if(contact.secondRelation > 0 && relationList != null){
        for(var entry in relationList!.entries) {
          if (entry.key == '${contact.secondRelation}'){
            selectRelation2.value = KeyValue(int.tryParse(entry.key) ?? 0,entry.value);
          }
        }
      }
    }
 }

  /// 选择关系 type： 0为第一联系人，1为第二联系人
  void chooseRelation(int type){
    hintKeyboard();
    if(relationList == null || relationList == null){
      getConstant();
      return;
    }

    Get.bottomSheet(
      DialogKeyValue(relationList, (code,value){
        if(type == 0){
          selectRelation1.value = KeyValue(code,value);
        }else{
          selectRelation2.value = KeyValue(code,value);
        }
      }),
    );
  }

  /// 上一步
  void back() {
    BaseInfoController c = Get.find();
    c.setCurrentIndex(1);
  }

  /// 提交
  void submit() async{
    if (phone1Controller.text.isEmpty || !phone1Controller.text.isPhoneNumber){
      EasyLoading.showToast('请输入常用联系人的正确手机号');
      return;
    }
    if (name1Controller.text.isEmpty || name1Controller.text.isEmpty){
      EasyLoading.showToast('请输入常用联系人的姓名');
      return;
    }
    if (selectRelation1.value.key == 0 || '${selectRelation1.value.value}'.isEmpty){
      EasyLoading.showToast('请选择常用联系人关系');
      return;
    }
    if (phone2Controller.text.isEmpty || !phone2Controller.text.isPhoneNumber){
      EasyLoading.showToast('请输入紧急联系人的正确手机号');
      return;
    }
    if (name2Controller.text.isEmpty || name2Controller.text.isEmpty){
      EasyLoading.showToast('请输入紧急联系人的姓名');
      return;
    }
    if (selectRelation2.value.key == 0 || '${selectRelation2.value.value}'.isEmpty){
      EasyLoading.showToast('请选择紧急联系人关系');
      return;
    }
    if (phone1Controller.text == phone2Controller.text){
      EasyLoading.showToast('常用联系人与紧急联系人手机号不可以相同');
      return;
    }
    if (selectRelation1.value.key == selectRelation2.value.key){
      EasyLoading.showToast('常用联系人与紧急联系人关系不可以相同');
      return;
    }

    Map<String, dynamic> obj = {
      'first_phone' : phone1Controller.text,
      'first_name' : name1Controller.text,
      'first_relation' : selectRelation1.value.key,
      'second_phone' : phone2Controller.text,
      'second_name' : name2Controller.text,
      'second_relation' : selectRelation2.value.key,
    };
    var result = await apiProvider.getData(obj, ApiProvider.contact_updated);
    if(result.code == 200){
      EasyLoading.showToast('保存成功');
      Get.back();
    }
  }
}
